/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shxzz.easy12306.services.pay.common.enums;

import com.shxzz.easy12306.framework.starter.convention.exceptions.ServiceException;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

/**
 * 交易状态枚举
 */
@Getter
public enum TradeStatusEnum {

    /**
     * 交易创建，等待买家付款
     */
    WAIT_BUYER_PAY(0, List.of("WAIT_BUYER_PAY")),

    /**
     * 未付款交易超时关闭，或支付完成后全额退款/部分退款
     */
    TRADE_CLOSED(10, List.of("TRADE_CLOSED")),

    /**
     * 交易支付成功
     */
    TRADE_SUCCESS(20, List.of("TRADE_SUCCESS")),

    /**
     * 交易结束，不可退款
     */
    TRADE_FINISHED(30, List.of("TRADE_FINISHED")),
    ;

    /**
     * 获取交易状态码
     */
    private final Integer tradeCode;

    /**
     * 对应各种支付方式交易状态集合
     */
    private final List<String> tradeStatus;

    TradeStatusEnum(Integer tradeCode, List<String> tradeStatus) {
        this.tradeCode = tradeCode;
        this.tradeStatus = tradeStatus;
    }

    /**
     * 查询真实的交易状态
     *
     * @param tradeStatus 三方交易状态
     * @return 真实的交易状态，入库使用
     */
    public static Integer findTradeCode(String tradeStatus) {
        return Arrays.stream(TradeStatusEnum.values())
                .filter(each -> each.getTradeStatus().contains(tradeStatus))
                .findFirst()
                .map(each -> each.tradeCode)
                .orElseThrow(() -> new ServiceException("未找到支付状态"));
    }
}
